package ynu.edu.dish.controller;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import ynu.edu.common.result.Result;
import ynu.edu.dish.dto.DishSpecDTO;
import ynu.edu.dish.service.DishSpecService;
import ynu.edu.dish.vo.DishSpecVO;

import java.util.List;

/**
 * 菜品规格控制器
 */
@RestController
@RequestMapping("/dish-specs")
@RequiredArgsConstructor
public class DishSpecController {

    private final DishSpecService dishSpecService;

    /**
     * 创建菜品规格
     */
    @PostMapping
    public Result<DishSpecVO> createDishSpec(@RequestBody @Valid DishSpecDTO dishSpecDTO) {
        DishSpecVO dishSpecVO = dishSpecService.createDishSpec(dishSpecDTO);
        return Result.success(dishSpecVO);
    }

    /**
     * 更新菜品规格
     */
    @PutMapping("/{id}")
    public Result<DishSpecVO> updateDishSpec(@PathVariable Long id, @RequestBody @Valid DishSpecDTO dishSpecDTO) {
        dishSpecDTO.setId(id);
        DishSpecVO dishSpecVO = dishSpecService.updateDishSpec(dishSpecDTO);
        return Result.success(dishSpecVO);
    }

    /**
     * 获取菜品规格信息
     */
    @GetMapping("/{id}")
    public Result<DishSpecVO> getDishSpecById(@PathVariable Long id) {
        DishSpecVO dishSpecVO = dishSpecService.getDishSpecById(id);
        return Result.success(dishSpecVO);
    }

    /**
     * 删除菜品规格
     */
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteDishSpec(@PathVariable Long id) {
        boolean result = dishSpecService.deleteDishSpec(id);
        return Result.success(result);
    }

    /**
     * 根据菜品ID查询规格列表
     */
    @GetMapping("/dish/{dishId}")
    public Result<List<DishSpecVO>> getDishSpecsByDishId(@PathVariable Long dishId) {
        List<DishSpecVO> dishSpecVOs = dishSpecService.getDishSpecsByDishId(dishId);
        return Result.success(dishSpecVOs);
    }

    /**
     * 根据菜品ID和状态查询规格列表
     */
    @GetMapping("/dish/{dishId}/status/{status}")
    public Result<List<DishSpecVO>> getDishSpecsByDishIdAndStatus(
            @PathVariable Long dishId,
            @PathVariable Integer status) {
        List<DishSpecVO> dishSpecVOs = dishSpecService.getDishSpecsByDishIdAndStatus(dishId, status);
        return Result.success(dishSpecVOs);
    }

    /**
     * 更新菜品规格状态
     */
    @PutMapping("/{id}/status")
    public Result<Boolean> updateDishSpecStatus(
            @PathVariable Long id,
            @RequestParam Integer status) {
        boolean result = dishSpecService.updateDishSpecStatus(id, status);
        return Result.success(result);
    }

    /**
     * 批量创建菜品规格
     */
    @PostMapping("/batch")
    public Result<List<DishSpecVO>> batchCreateDishSpecs(@RequestBody List<DishSpecDTO> dishSpecDTOs) {
        List<DishSpecVO> dishSpecVOs = dishSpecService.batchCreateDishSpecs(dishSpecDTOs);
        return Result.success(dishSpecVOs);
    }
} 